package com.the4man.framework.util;

import com.the4man.framework.model.Dictionary;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
public class DictionaryUtils {
    private static final Logger logger = LoggerFactory.getLogger(DictionaryUtils.class);

    public DictionaryUtils() {
    }

    public static List<Dictionary> getDictionary(Dictionary querycondition) {
        List<Dictionary> retDictionaryList = new ArrayList();
        if(querycondition == null) {
            logger.warn("查询条件为空");
            return (List)retDictionaryList;
        } else {
            Class<?> clazz = null;
            Field[] allFields = null;
            Method getMethod = null;
            List<Dictionary> allDictionaryList = DictionaryData.allDictionaryList;
            if(logger.isDebugEnabled()) {
                logger.debug("内存中字典表数据行数为: " + allDictionaryList.size());
                logger.debug("查询条件为: " + querycondition);
            }

            try {
                clazz = querycondition.getClass();
                allFields = clazz.getDeclaredFields();
                List<Dictionary> prixList = new ArrayList();
                Field[] var10 = allFields;
                int var9 = allFields.length;

                for(int var8 = 0; var8 < var9; ++var8) {
                    Field field = var10[var8];
                    List<Dictionary> thisList = new ArrayList();
                    field.setAccessible(true);
                    Object value = field.get(querycondition);
                    String fieldName = field.getName();
                    getMethod = clazz.getDeclaredMethod(initMethodName("get", fieldName), new Class[0]);
                    if(value != null && !"".equals(value)) {
                        Iterator var15 = allDictionaryList.iterator();

                        while(var15.hasNext()) {
                            Dictionary dictionary = (Dictionary)var15.next();
                            if(value.equals(getMethod.invoke(dictionary, new Object[0]))) {
                                thisList.add(dictionary);
                            }
                        }
                    }

                    if(!CollectionUtils.isEmpty((Collection)prixList) && !CollectionUtils.isEmpty(thisList)) {
                        prixList = (List)CollectionUtils.intersection((Collection)prixList, thisList);
                    } else {
                        ((List)prixList).addAll(thisList);
                    }

                    retDictionaryList = prixList;
                }
            } catch (Exception var16) {
                logger.error("内存中获取字典表数据错误,查询条件为 : " + querycondition.toString(), var16);
            }

            return (List)retDictionaryList;
        }
    }

    public static <T> void replaceCodeValue(List<T> sourceList, Dictionary querycondition, String sourceFieldName, String targetFieldName) {
        if(!CollectionUtils.isEmpty(sourceList) && sourceFieldName != null && targetFieldName != null) {
            List<Dictionary> dictionaryList = getDictionary(querycondition);
            if(logger.isDebugEnabled()) {
                logger.debug("查询出字典表中的数据行数为: " + dictionaryList.size());
                logger.debug("和字典表CodeValue关联的字段名为: " + sourceFieldName);
                logger.debug("要展示在列表中的字段名为: " + targetFieldName);
            }

            Class<?> clazz = sourceList.get(0).getClass();
            Field sourceFiled = null;
            Method targetMethod = null;

            try {
                Iterator var9 = dictionaryList.iterator();

                while(var9.hasNext()) {
                    Dictionary dictionary = (Dictionary)var9.next();
                    Iterator var11 = sourceList.iterator();

                    while(var11.hasNext()) {
                        T source = (T) var11.next();
                        sourceFiled = clazz.getDeclaredField(sourceFieldName);
                        sourceFiled.setAccessible(true);
                        Object value = sourceFiled.get(source);
                        if(value != null) {
                            targetMethod = clazz.getDeclaredMethod(initMethodName("set", targetFieldName), new Class[]{String.class});
                            if(value.equals(dictionary.getCodeValue())) {
                                targetMethod.invoke(source, new Object[]{dictionary.getCodeName()});
                            }
                        }
                    }
                }
            } catch (Exception var13) {
                logger.error("关联字典表的codeValue字段取值错误", var13);
            }

        }
    }

    private static String initMethodName(String prix, String fieldName) {
        StringBuilder methodName = new StringBuilder();
        methodName.append(prix);
        methodName.append(fieldName.substring(0, 1).toUpperCase());
        methodName.append(fieldName.substring(1));
        return methodName.toString();
    }
}
